package com.example.youjingjing.electronicbalance.widge;

import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;

import com.example.youjingjing.electronicbalance.R;
import com.example.youjingjing.electronicbalance.presenter.RegisterPresenter;
import com.example.youjingjing.electronicbalance.view.RegisterView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class RegisterActivity extends BaseActivity<RegisterView,RegisterPresenter> implements RegisterView {
    @BindView(R.id.register_edit_name)
    EditText etUserName;
    @BindView(R.id.register_edit_psw)
    EditText etPassword;
    @BindView(R.id.register_edit_ver)
    EditText etVer;

    @BindView(R.id.register_textInputLayout_name)
    TextInputLayout textInputLayout_name;

    @BindView(R.id.register_textInputLayout_key)
    TextInputLayout textInputLayout_key;

    @BindView(R.id.register_textInputLayout_Ver)
    TextInputLayout textInputLayout_ver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        //设置可以计数
        textInputLayout_key.setCounterEnabled(true);
        //计数的最大值
        textInputLayout_key.setCounterMaxLength(6);

        etPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                checkPswd(etPassword.getText().toString(),true);
            }
        });
        etUserName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                checkName(etUserName.getText().toString(),true);
            }
        });
        etVer.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                checkVer(etUserName.getText().toString(),true);
            }
        });
    }

    /**
     *若用户名为空且用户是想登录，提示错误，返回false。不为空，错误设为null
     */
    private boolean checkName(CharSequence name,boolean isLogin) {
        if(TextUtils.isEmpty(name)) {
            if(isLogin) {
                textInputLayout_name.setError("用户名不能为空");
                return false;
            }
        }else{
            textInputLayout_name.setError(null);
        }
        return true;
    }
    private boolean checkPswd(CharSequence pswd,boolean isLogin) {
        if(TextUtils.isEmpty(pswd)) {
            if(isLogin) {
                textInputLayout_key.setError("密码不能为空");
                return false;
            }
        }else{
            textInputLayout_key.setError(null);
        }
        return true;
    }private boolean checkVer(CharSequence pswd,boolean isLogin) {
        if(TextUtils.isEmpty(pswd)) {
            if(isLogin) {
                textInputLayout_ver.setError("验证码不能为空");
                return false;
            }
        }else{
            textInputLayout_key.setError(null);
        }
        return true;
    }

    @Override
    protected RegisterPresenter createPresenter() {
        return new RegisterPresenter();
    }

    @Override
    public void showStatusTip(int status) {

    }

    @OnClick(R.id.register_tv_login)
    public void onClick(){
        jumpActivity(LoginActivity.class);
    }

    @OnClick(R.id.register_btn_commit)
    public void register(){
        if(!checkName(etUserName.getText(),true))
            return;
        if(!checkPswd(etPassword.getText(),true))
            return;
        if(!checkVer(etVer.getText(),true))
            return;
        presenter.register(etUserName.getText().toString(), etPassword.getText().toString(),etVer.getText().toString());
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        jumpActivity(MainActivity.class);
    }
}
